<?php
#+------------------------------------------------------------------
#| 我曾七次鄙视自己的灵魂:
#| 第一次,当它本可进取时，却故作谦卑；
#| 第二次,当它空虚时，用爱欲来填充；
#| 第三次,在困难和容易之间，它选择了容易；
#| 第四次,它犯了错，却借由别人也会犯错来宽慰自己；
#| 第五次,它自由软弱，却把它认为是生命的坚韧；
#| 第六次,当它鄙夷一张丑恶的嘴脸时，却不知那正是自己面具中的一副；
#| 第七次,它侧身于生活的污泥中虽不甘心，却又畏首畏尾。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace app;

use think\App;
use think\facade\Cookie;

class Response
{

	const JSON     = 'json';
	const VIEW     = 'view';
	const REDIRECT = 'redirect';
	/**
	 * @var string
	 */
	protected $contentType;

	protected $app;


	public function __construct(App $app)
	{
		$this->app = $app;
		$this->contentType = $this->getResponseType();
	}

	/**
	 * 成功
	 * @param array $data 数据
	 * @param int $code 状态码
	 * @param string $message 错误信息
	 * @param string|null $url 跳转地址
	 * @param int $wait 等待时间
	 * @param array $header 头信息
	 * @return \think\Response
	 */
	public function success(array $data = [],  string $message = '', string $url = null , array $header = [], int $code = 200,int $wait = 3)
	{
		if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
			$url = $_SERVER["HTTP_REFERER"];
		} elseif ($url) {
			$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
		}
		$contentType = $this->contentType;

		$data = [
			'code'    => $code,
			'message' => $message,
			'data'    => $data,
			'wait'    => $wait,
			'url'     => $contentType == self::JSON ? (string)$url:$url
		];
		if ($contentType === self::VIEW) {
			return \think\Response::create($this->app->config->get('app.dispatch_jump_tmpl'), $contentType)->header($header)->assign($data);
		}
		return \think\Response::create($data, $contentType, 200)->header($header);
	}

	/**
	 * 失败
	 * @param string $message
	 * @param string|null $url
	 * @param array $data
	 * @param int $code
	 * @param int $wait
	 * @param array $header
	 * @return \think\Response
	 */
	public function error(string $message = '', string $url = null, array $data = [], int $code = 500, int $wait = 3, array $header = [])
	{
		if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
			$url = $_SERVER["HTTP_REFERER"];
		} elseif ($url) {
			$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
		}

		$contentType = $this->contentType;

		$data = [
			'code'    => $code,
			'error' => $message,
			'data'    => $data,
			'wait'    => $wait,
			'message' => $message,
			'url'     => $contentType == self::JSON?(string)$url:$url
		];

		if ($contentType === self::VIEW) {
			return \think\Response::create($this->app->config->get('app.dispatch_jump_tmpl'), $contentType)->header($header)->assign($data);
		}
		return \think\Response::create($data, $contentType,200)->header($header);
	}

	/**
	 * 重定向
	 * @param string $url
	 * @param array $header
	 * @return \think\Response
	 */
	public function redirect(string $url, array $header = [])
	{
		return \think\Response::create($url, self::REDIRECT,302)->header($header);
	}

	/**
	 * 获取当前请求类型
	 * @return string
	 */
	public function getResponseType()
	{
		if ('api' === app('http')->getName()) {
			return self::JSON;
		}
		return $this->app->request->isJson() || $this->app->request->isAjax() ? self::JSON : self::VIEW;
	}
}
